/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi/native_api.h"
#include <vector>
#include <string>
#include "mars/comm/xlogger/xlogger.h"
#include "mars/comm/napi/util/var_cache.h"
#include "mars/comm/napi/util/napi_common.h"
#include "mars/comm/napi/util/comm_function.h"
#include "mars/log/appender.h"
#include "mars/log/xlogger_interface.h"
#include "hilog/log.h"
#define LOG_DOMAIN 0x0201
#define LOG_TAG "MY_TAG"
#define LONGTHREADID2INT(a) ((a >> 32)^((a & 0xFFFF)))

#define CHECK_LOG_INSTANCE(log_instance) \
    if (log_instance < 0) {\
        delete[] args;\
        napi_create_int32(env, -1, &result);\
        return result;\
    }

extern "C" {

std::string _nameprefix;

//JNIEXPORT jlong JNICALL Java_com_ohos_mars_xlog_Xlog_newXlogInstance(JNIEnv *env, jobject, jobject _log_config)
napi_value JS_com_ohos_mars_xlog_Xlog_newXlogInstance(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value *args = new napi_value[1];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 1) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

    napi_value _log_config = args[0];

    if (NULL == _log_config) {
        xerror2(TSF"logconfig is null");
        delete[] args; 
		napi_create_int32(env, -2, &result);
    	return result;
    }

    
    int level;
    int status = JS_GetProperty(env, _log_config, "level", &level);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -3, &result);
		return result;
    }

    int mode;
    status = JS_GetProperty(env, _log_config, "mode", &mode);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -4, &result);
		return result;
    }
    
    std::string logdir;
    status = JS_GetProperty(env, _log_config, "logdir", &logdir);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -5, &result);
		return result;
    }
    
    std::string nameprefix;
    status = JS_GetProperty(env, _log_config, "nameprefix", &nameprefix);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -6, &result);
		return result;
    }

    std::string pubkey;
    status = JS_GetProperty(env, _log_config, "pubkey", &pubkey);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -7, &result);
		return result;
    }

    int compressmode;    
    status = JS_GetProperty(env, _log_config, "compressmode", &compressmode);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -8, &result);
		return result;
    }

    int compresslevel;    
    status = JS_GetProperty(env, _log_config, "compresslevel", &compresslevel);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -9, &result);
		return result;
    }

    std::string cachedir;    
    status = JS_GetProperty(env, _log_config, "cachedir", &cachedir);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -10, &result);
		return result;
    }

    int cachedays;     
    status = JS_GetProperty(env, _log_config, "cachedays", &cachedays);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -11, &result);
		return result;
    }

    mars::xlog::XLogConfig config = {(mars::xlog::TAppenderMode)mode, logdir, nameprefix, pubkey,
                                     (mars::xlog::TCompressMode)compressmode, compresslevel, cachedir, cachedays};
    mars::comm::XloggerCategory* category = mars::xlog::NewXloggerInstance(config, (TLogLevel)level);
    if (nullptr == category) {
        delete[] args;
        napi_create_int32(env, -12, &result);
		return result;
    }
    delete[] args;
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT jlong JNICALL Java_com_ohos_mars_xlog_Xlog_getXlogInstance(JNIEnv *env, jobject, jstring _nameprefix)
napi_value JS_com_ohos_mars_xlog_Xlog_getXlogInstance(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value *args = new napi_value[1];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 1) {
        delete[] args; 
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    int status = GetNAPIStringValue(env, args[0], _nameprefix);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int64(env, -2, &result);
		return result;
    }

    mars::comm::XloggerCategory* category = mars::xlog::GetXloggerInstance(_nameprefix.c_str());
    if (nullptr == category) {
        delete[] args;
        napi_create_int64(env, -1, &result);
    }

    delete[] args;
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_releaseXlogInstance(JNIEnv *env, jobject, jstring _nameprefix)
napi_value JS_com_ohos_mars_xlog_Xlog_releaseXlogInstance(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value *args = new napi_value[1];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 1) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. String expected.");

    int status = GetNAPIStringValue(env, args[0], _nameprefix);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -2, &result);
		return result;
    }

    mars::xlog::ReleaseXloggerInstance(_nameprefix.c_str());
    delete[] args;
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_appenderOpen(JNIEnv *env, jclass clazz, jobject _log_config)
napi_value JS_com_tenScent_mars_xlog_Xlog_appenderOpen(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value *args = new napi_value[1];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 1) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

    napi_value _log_config = args[0];

    if (NULL == _log_config) {
        xerror2(TSF"logconfig is null");
        napi_create_int32(env, -2, &result);
    	return result;
    }

    int level;
    int status = JS_GetProperty(env, _log_config, "level", &level);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -3, &result);
		return result;
    }

    int mode;
    status = JS_GetProperty(env, _log_config, "mode", &mode);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -4, &result);
		return result;
    }
    OH_LOG_INFO(LOG_APP, "appenderOpen  mode %d",mode);

    std::string logdir;
    status = JS_GetProperty(env, _log_config, "logdir", logdir);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -5, &result);
		return result;
    }
    OH_LOG_INFO(LOG_APP, "appenderOpen  logdir %s",logdir.c_str());

    std::string nameprefix;
    status = JS_GetProperty(env, _log_config, "nameprefix", nameprefix);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -6, &result);
		return result;
    }

    std::string pubkey;
    status = JS_GetProperty(env, _log_config, "pubkey", pubkey);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -6, &result);
		return result;
    }
    
    int compressmode;
    status = JS_GetProperty(env, _log_config, "compressmode", &compressmode);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -7, &result);
		return result;
    }
    
    int compresslevel;
    status = JS_GetProperty(env, _log_config, "compresslevel", &compresslevel);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -8, &result);
		return result;
    }

    std::string cachedir;
    status = JS_GetProperty(env, _log_config, "cachedir", cachedir);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -9, &result);
		return result;
    }

    int cachedays;
    status = JS_GetProperty(env, _log_config, "cachedays", &cachedays);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -10, &result);
		return result;
    }

    mars::xlog::XLogConfig config = {(mars::xlog::TAppenderMode)mode, logdir, nameprefix, pubkey,
                                     (mars::xlog::TCompressMode)compressmode, compresslevel, cachedir, cachedays};

    appender_open(config);
	xlogger_SetLevel((TLogLevel)level);
    delete[] args;
    napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_appenderClose(JNIEnv *env, jobject)
napi_value JS_com_ohos_mars_xlog_Xlog_appenderClose(napi_env env, napi_callback_info info)
{
    mars::xlog::appender_close();
    napi_value result;
    napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_appenderFlush(JNIEnv *env, jobject, jlong _log_instance_ptr, jboolean _is_sync)
napi_value JS_com_ohos_mars_xlog_Xlog_appenderFlush(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_boolean, "Wrong argument type. Boolean expected.");

	int64_t _log_instance_ptr;
    bool _is_sync;
    napi_status status;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
        delete[] args;
		napi_create_int32(env, -2, &result);
		return result;
	}

    if (napi_ok != napi_get_value_bool(env, args[1], &_is_sync)) {
        delete[] args;
		napi_create_int32(env, -3, &result);
		return result;
	}
    CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::Flush(_log_instance_ptr, _is_sync);
    napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_logWrite(JNIEnv *env, jclass, jobject _log_info, jstring _log)
napi_value JS_com_ohos_mars_xlog_Xlog_logWrite(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();

    size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. Number expected.");    

    napi_value _log_info = args[0];
    std::string _log;

    int status = GetNAPIStringValue(env, args[1], _log);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -2, &result);
		return result;
    }

    if (NULL == _log_info || _log.size() == 0) {
        xerror2(TSF"loginfo or log is null");
        delete[] args;
        napi_create_int32(env, -3, &result);
		return result;
    }

    int level;
    status = JS_GetProperty(env, _log_info, "level", &level);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -4, &result);
		return result;
    }       
    
    if (!xlogger_IsEnabledFor((TLogLevel)level)) {
        delete[] args;
        napi_create_int32(env, -5, &result);
        return result;
    }
    
    std::string tag;
    status = JS_GetProperty(env, _log_info, "tag", tag);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -5, &result);
		return result;
    }    
    
    std::string filename;
    status = JS_GetProperty(env, _log_info, "filename", filename);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -6, &result);
		return result;
    }    

    
    std::string funcname;
    status = JS_GetProperty(env, _log_info, "funcname", funcname);    
	if (status != 0) {
        delete[] args;
        napi_create_int32(env, -7, &result);
		return result;
    }

    int line;
    status = JS_GetProperty(env, _log_info, "line", &line);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -8, &result);
		return result;
    }
    
    int64_t pid;
    status = JS_GetProperty(env, _log_info, std::string("pid"), &pid);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -9, &result);
		return result;
    }

    int64_t tid;
    status = JS_GetProperty(env, _log_info, "tid", &tid);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -10, &result);
		return result;
    }

    int64_t maintid;
    status = JS_GetProperty(env, _log_info, "maintid", &maintid);
    if (status != 0) {
        delete[] args;
        napi_create_int32(env, -11, &result);
		return result;
    }

    XLoggerInfo xlog_info;
    gettimeofday(&xlog_info.timeval, NULL);
    xlog_info.level = (TLogLevel)level;
    xlog_info.line = line;
    xlog_info.pid = (int)pid;
    xlog_info.tid = LONGTHREADID2INT(tid);
    xlog_info.maintid = LONGTHREADID2INT(maintid);;

    xlog_info.tag = tag.c_str();
    xlog_info.filename = filename.c_str();
    xlog_info.func_name = funcname.c_str();

    xlogger_Write(&xlog_info, _log.c_str());

    delete[] args;
    napi_create_int32(env, 0, &result);
	return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_logWrite2(JNIEnv *env, jclass, jlong _log_instance_ptr, int _level, jstring _tag, jstring _filename,
 //         jstring _funcname, jint _line, jint _pid, jlong _tid, jlong _maintid, jstring _log)
napi_value JS_com_ohos_mars_xlog_Xlog_logWrite2(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 10; 
    size_t argc = 10; 
    napi_value *args = new napi_value[10];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc < 10) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[2], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[3], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[4], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[5], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[6], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[7], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[8], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

    NAPI_CALL(env, napi_typeof(env, args[9], &valueType));
	NAPI_ASSERT(env, valueType == napi_string, "Wrong argument type. Number expected.");
	int64_t _log_instance_ptr;
    int _level;
    std::string _tag, _filename, _funcname;
    int _line, _pid;
    int64_t _tid, _maintid;
    std::string _log;
    int status;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
        delete[] args;
		napi_create_int32(env, -2, &result);
		return result;
	}

    if (napi_ok != napi_get_value_int32(env, args[1], &_level)) {
        delete[] args;
		napi_create_int32(env, -3, &result);
		return result;
	}

	status = GetNAPIStringValue(env, args[2], _tag);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -4, &result);
		return result;
    }

    status = GetNAPIStringValue(env, args[3], _filename);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -5, &result);
		return result;
    }

    status = GetNAPIStringValue(env, args[4], _funcname);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -6, &result);
		return result;
    }

    if (napi_ok != napi_get_value_int32(env, args[5], &_line)) {
        delete[] args;
		napi_create_int32(env, -7, &result);
		return result;
	}
    
    if (napi_ok != napi_get_value_int32(env, args[6], &_pid)) {
        delete[] args;
		napi_create_int32(env, -8, &result);
		return result;
	}

    if (napi_ok != napi_get_value_int64(env, args[7], &_tid)) {
        delete[] args;
		napi_create_int32(env, -9, &result);
		return result;
	}

    if (napi_ok != napi_get_value_int64(env, args[8], &_maintid)) {
        delete[] args;
		napi_create_int32(env, -10, &result);
		return result;
	}

    status = GetNAPIStringValue(env, args[9], _log);
	if (status != napi_ok) {
        delete[] args;
        napi_create_int32(env, -11, &result);
		return result;
    }

    CHECK_LOG_INSTANCE(_log_instance_ptr);

    if (!mars::xlog::IsEnabledFor(_log_instance_ptr, (TLogLevel)_level)) {
        delete[] args;
        napi_create_int32(env, -12, &result);
        return result;
    }

    XLoggerInfo xlog_info;
    gettimeofday(&xlog_info.timeval, NULL);
    xlog_info.level = (TLogLevel)_level;
    xlog_info.line = (int)_line;
    xlog_info.pid = (int)_pid;
    xlog_info.tid = LONGTHREADID2INT(_tid);
    xlog_info.maintid = LONGTHREADID2INT(_maintid);

    const char* tag_cstr = NULL;
    const char* filename_cstr = NULL;
    const char* funcname_cstr = NULL;
    const char* log_cstr = NULL;

    if (_tag.size() != 0) {
        tag_cstr = _tag.c_str();
    }

    if (_filename.size() != 0) {
        filename_cstr = _filename.c_str();
    }

    if (_funcname.size() != 0) {
        funcname_cstr = _funcname.c_str();
    }

    if (_log.size() != 0) {
        log_cstr = _log.c_str();
    }

    xlog_info.tag = NULL == tag_cstr ? "" : tag_cstr;
    xlog_info.filename = NULL == filename_cstr ? "" : filename_cstr;
    xlog_info.func_name = NULL == funcname_cstr ? "" : funcname_cstr;

    mars::xlog::XloggerWrite(_log_instance_ptr, &xlog_info, NULL == log_cstr ? "NULL == log" : log_cstr);
    napi_create_int32(env, 0, &result);
    delete[] args;

    return result;
}

//JNIEXPORT jint JNICALL Java_com_ohos_mars_xlog_Xlog_getLogLevel(JNIEnv *, jobject, jlong _log_instance_ptr)
napi_value JS_com_ohos_mars_xlog_Xlog_getLogLevel(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 1; 
    size_t argc = 1; 
    napi_value *args = new napi_value[1];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 1) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}

    if (_log_instance_ptr < 0) {
        napi_create_int32(env, kLevelNone, &result);
        return result;
    } else {
        napi_create_int32(env, mars::xlog::GetLevel(_log_instance_ptr), &result);
        return result;
    }    
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_setLogLevel(JNIEnv *, jobject, jlong _log_instance_ptr, jint _log_level)
napi_value JS_com_ohos_mars_xlog_Xlog_setLogLevel(napi_env env, napi_callback_info info)
{    
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. String expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}
	int _log_level;
	if (napi_ok != napi_get_value_int32(env, args[1], &_log_level)) {
		napi_create_int32(env, -3, &result);
		return result;
	}
    CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::SetLevel(_log_instance_ptr, (TLogLevel)_log_level);
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_setAppenderMode(JNIEnv *, jobject, jlong _log_instance_ptr, jint _mode)
napi_value JS_com_ohos_mars_xlog_Xlog_setAppenderMode(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. String expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}
	int _mode;
	if (napi_ok != napi_get_value_int32(env, args[1], &_mode)) {
		napi_create_int32(env, -3, &result);
		return result;
	}	
    CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::SetAppenderMode(_log_instance_ptr, (mars::xlog::TAppenderMode)_mode);
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_setConsoleLogOpen(JNIEnv *env, jobject, jlong _log_instance_ptr, jboolean _is_open)
napi_value JS_com_ohos_mars_xlog_Xlog_setConsoleLogOpen(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_boolean, "Wrong argument type. String expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}
	bool _is_open;
	if (napi_ok != napi_get_value_bool(env, args[1], &_is_open)) {
		napi_create_int32(env, -3, &result);
		return result;
	}	
    CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::SetConsoleLogOpen(_log_instance_ptr, _is_open);
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_setMaxFileSize(JNIEnv *env, jobject, jlong _log_instance_ptr, jlong _max_size)
napi_value JS_com_ohos_mars_xlog_Xlog_setMaxFileSize(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. String expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}
	int64_t _max_size;
	if (napi_ok != napi_get_value_int64(env, args[1], &_max_size)) {
		napi_create_int32(env, -3, &result);
		return result;
	}
	CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::SetMaxFileSize(_log_instance_ptr, _max_size);
    napi_create_int32(env, 0, &result);
    return result;
}

//JNIEXPORT void JNICALL Java_com_ohos_mars_xlog_Xlog_setMaxAliveTime(JNIEnv *env, jobject, jlong _log_instance_ptr, jlong _max_time)
napi_value JS_com_ohos_mars_xlog_Xlog_setMaxAliveTime(napi_env env, napi_callback_info info)
{
    VarCache* cacheInstance = VarCache::Singleton();
    napi_env jsenv = cacheInstance->GetJSEnv();
    
	size_t requireArgc = 2; 
    size_t argc = 2; 
    napi_value *args = new napi_value[2];
    napi_value result;
    NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));  

	if (argc != 2) {
		napi_create_int32(env, -1, &result);
    	return result;
	}

	napi_valuetype valueType;
	NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. Number expected.");

	NAPI_CALL(env, napi_typeof(env, args[1], &valueType));
	NAPI_ASSERT(env, valueType == napi_number, "Wrong argument type. String expected.");

	int64_t _log_instance_ptr;
   	if (napi_ok != napi_get_value_int64(env, args[0], &_log_instance_ptr)) {
		napi_create_int32(env, -2, &result);
		return result;
	}
	int64_t _max_time;
	if (napi_ok != napi_get_value_int64(env, args[1], &_max_time)) {
		napi_create_int32(env, -3, &result);
		return result;
	}
	CHECK_LOG_INSTANCE(_log_instance_ptr);
    mars::xlog::SetMaxAliveTime(_log_instance_ptr, _max_time);
    napi_create_int32(env, 0, &result);
    return result;
}
}

void ExportXlog() {}
